package com.itheima.file;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

//目标：实现客户端上传任意类型的文件数据给服务端保存起来
public class Client {
    public static void main(String[] args) {
        try(
                InputStream is=new FileInputStream("C:\\Users\\dell\\IdeaProjects\\file-io-app\\bio_demo\\src\\F15.png");
                ){
            Socket socket=new Socket(InetAddress.getLocalHost().getHostAddress(),9999);
            DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
            String fileName=new File("C:\\Users\\dell\\IdeaProjects\\file-io-app\\bio_demo\\src\\F15.png").getName();
            //每次传输文件之前先传输文件名称
            System.out.println(fileName.substring(fileName.lastIndexOf("."),fileName.length()));
            dos.writeUTF(fileName.substring(fileName.lastIndexOf("."),fileName.length()));
            //按照1kb的大小每次传输
            byte[] buffer=new byte[1024];
            int len;
            while((len=is.read(buffer))!=-1){
                dos.write(buffer,0,len);
            }
            dos.flush();
            //文件传输结束
            socket.shutdownOutput();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
